# -*- coding: utf-8 -*-

import re
import pandas as pd


LETTER = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T',
          'U','V','W','X','Y','Z','AA']

def letter2num(letter:str) -> int:
    return LETTER.index(letter)


def get_part(df:pd.DataFrame, position:str) -> pd.DataFrame:
    """
    df: 总 df
    position: 表格索引信息位置, eg: A32 中存放的是 21,A:C
    return: 子表
    """
    col,row,_ = re.split(r'(\d+)', position)
    row = int(row)

    info = df.iloc[row-1,letter2num(col)]
    nrows,cols_str = info.split(',')
    col_start,col_end = cols_str.split(':')
    ret = df.iloc[row:row+int(nrows)+1,letter2num(col_start):letter2num(col_end)+1]
    ret.columns = ret.values.tolist()[0]
    ret.drop([row],inplace=True)

    return ret

